(function () {
    window.domHelper = {
        doms: [],
        init: function (game) {
            this.game = game;
        },
        createFullScreenDiv: function (style) {
            var div = document.createElement("div");
            div.style.position = "absolute";
            div.style.top = 0;
            div.style.left = 0;
            div.style.width = "100%";
            div.style.height = "100%";
            for (var key in style) {
                div.style[key] = style[key];
            }

            return div;
        },
        domFollowNode: function (dom, node) {
            var ph = document.body.clientWidth / 750;
            var origin = { //网页中点。
                x: document.body.clientWidth / 2,
                y: document.body.clientHeight / 2
            };

            var pos = this.game.node.convertToNodeSpaceAR(node.convertToWorldSpaceAR(cc.Vec2.ZERO));
            var width = node.width * node.scale * ph;
            var height = node.height * node.scale * ph;
            dom.style.width = width + "px";
            dom.style.height = height + "px";
            // dom.style.opacity = node.opacity / 255;
            dom.style.left = origin.x + pos.x * ph - width / 2 + "px";
            dom.style.top = origin.y - pos.y * ph - height / 2 + "px";
        },
        setPositionPh: function (dom, x, y) {
            var ph = document.body.clientWidth / 750;
            var origin = { //网页中点。
                x: document.body.clientWidth / 2,
                y: document.body.clientHeight / 2
            };
            dom.style.left = origin.x + x * ph - dom.clientWidth / 2 + "px";
            dom.style.top = origin.y + y * ph - dom.clientHeight / 2 + "px";
        },
        setSizePh: function (dom, w, h) {
            var ph = document.body.clientWidth / 750;

            dom.style.width = w * ph + "px";
            dom.style.height = h * ph + "px";
        },
        addBtn: function (cb, obj, style, phx, phy, phw, phh, debug) {
            var dom = document.createElement("div");
            document.body.appendChild(dom);
            this.doms.push(dom);
            dom.style.display = "block";
            dom.style.position = "absolute";
            if (debug) {
                dom.style.background = "#0f0";
                dom.style.opacity = 0.5;
            }
            for (var key in style) {
                dom.style[key] = style[key];
            }
            dom.phData = {
                x: phx,
                y: phy,
                width: phw,
                height: phh
            };
            dom.onclick = cb.bind(obj);
        },
        update: function (dt) {
            for (var i = 0; i < this.doms.length; i++) {
                var dom = this.doms[i];
                if (dom) {
                    if (dom.phData) {
                        this.setSizePh(dom, dom.phData.width, dom.phData.height);
                        this.setPositionPh(dom, dom.phData.x, dom.phData.y);
                    }
                }
            }
        },
        clean: function () {
            for (var i = 0; i < this.doms.length; i++) {
                var dom = this.doms[i];
                if (dom) {
                    document.body.removeChild(dom);
                }
            }
            this.doms = [];
        }
    };
})();